<?php

//广告字典
$ad_disc = array('ad_1','ad_2');

//指标及指标权重
$weight = array('index_1'=>'0.1','index_2'=>'0.1','index_3'=>'0.8');

//广告具体指标值
$ad     = array('ad_1'=>array('index_1'=>'1000','index_2'=>'300','index_3'=>'3'),'ad_2'=>array('index_1'=>'1000','index_2'=>'200','index_3'=>'2'));

//广告最终综合指标值
foreach($ad as $key=>$value)
{
    $ad_weight[$key] = $value['index_1'] * $weight['index_1'] + $value['index_2'] * $weight['index_2'] + $value['index_3'] * $weight['index_3'];
}

//所有广告指标值总和
$ad_weight_sum = array_sum($ad_weight);

//广告出现概率
foreach($ad as $key=>$value)
{
    $ad_per[$key] = number_format($ad_weight[$key] / $ad_weight_sum[$key],2) * 100;
}

//根据概率选取广告
foreach($ad_per as $key=>$value)
{
    $pNum = mt_rand(1, $pSum);
            
    if($pNum < $value)
    {
        $res = $ad_disc[$key];
        break;
    }
    else
    {
        $pSum -= $value;
        continue;
    }            
}

//返回结果
return $res;